angular.module('proListModule', [])
    .controller('proListCtr', ['$scope', 'httpFactory','$location','$stateParams', function ($scope, httpFactory,$location,$stateParams) {
        $scope.goBack=function(){
            window.history.back();
        };
        var searchVal=$stateParams.search;
        httpFactory.getData('./src/datas/proData.json')
            .then(function (data) {
                $scope.proListData = data;
                $scope.text=searchVal;
                $scope.searchGoods(searchVal);
            });
        /*搜索*/
        $scope.searchGoods=function(text){
            if(text){
                var reg=new RegExp(text,'i');/*i,不区分大小写*/
                $scope.newDatas=[];
                var searchTexts=LsySession.getItem('searchText');
                searchTexts=searchTexts?searchTexts:[];
                searchTexts.unshift(text);
                if(searchTexts.length>8){
                    searchTexts.pop();
                }
                LsySession.setItem('searchText',searchTexts);
                angular.forEach($scope.proListData,function(item){
                    if(reg.test(item.title)){
                        $scope.newDatas.push(item);
                    }
                })
            }else{
                $scope.newDatas=$scope.proListData;
            }
        };
        $scope.title = 'proList';
        $scope.colorIndex = 100;
        $scope.colors = ["蓝色", "紫色", "黑色", "红色", "白色", "灰色"];
        $scope.sort = 'id';
        $scope.color = '';
        var keepColor = '';
        var issales = false;
        var isprice = false;
        var ispraise = false;

        $scope.sortBy = function (type) {
            switch (type) {
                case 'sales':
                    $scope.sort = issales ? type : '-' + type;
                    issales = !issales;
                    break;
                case 'price':
                    $scope.sort = isprice ? type : '-' + type;
                    isprice = !isprice;
                    break;
                case 'praise':
                    $scope.sort = ispraise ? type : '-' + type;
                    ispraise = !ispraise;
                    break;
            }
        };
        $scope.changeColor = function (type,index) {
            keepColor = type;
            if (!$scope.color || $scope.color != type) {
                $scope.color = type;
                $scope.colorIndex = index;
            } else {
                if ($scope.color == type) {
                    $scope.color = '';
                    $scope.colorIndex = 100;
                } else {
                    $scope.color = type;
                    $scope.colorIndex = index;
                }
            }
        };
        /*立即购买*/
        $scope.buyNow=function(proid) {
            var confirmOrderData = [];
            var obj = {
                id: proid,
                num: 1
            };
            confirmOrderData.push(obj);
            LsyStorage.setItem('confirmOrderDatas', confirmOrderData);
            $location.path('confirm_order');
        }
    }]);
